import rand from './rand.js';
function generateSignedNetWork(
  nodeNum,
  communityNum,
  i_p_edgeNum,
  i_n_edgeNum,
  o_p_edgeNum,
  o_n_edgeNum
) {
  // if(nodeNum < 10) throw new Error('网络的节点个数必须大于10');
  if (
    i_p_edgeNum % 1 !== 0 ||
    i_n_edgeNum % 1 !== 0 ||
    o_p_edgeNum % 1 !== 0 ||
    o_n_edgeNum % 1 !== 0 ||
    nodeNum % 1 !== 0 ||
    communityNum % 1 !== 0
  ) {
    throw new Error('所有参数必须为正整数');
  }
  // 初始化邻接矩阵
  const matrix = new Array(nodeNum);
  for (let i = 0; i < nodeNum; i++) {
    matrix[i] = new Array(nodeNum).fill(0);
  }

  // 规划社区
  let communities = new Array(communityNum);
  for (let i = 0; i < communityNum; i++) {
    communities[i] = [];
  }
  for (let i = 0; i < nodeNum; i++) {
    communities[rand(0, communityNum - 1)].push(i);
  }

  // 添加社区内的正边
  for (let i = 0; i < i_p_edgeNum; i++) {
    let communityIndex, source, target;
    while (true) {
      communityIndex = rand(0, communityNum - 1);
      source = communities[communityIndex][rand(0, communities[communityIndex].length - 1)];
      target = communities[communityIndex][rand(0, communities[communityIndex].length - 1)];
      if (source !== target && matrix[source][target] === 0) {
        break;
      }
    }
    matrix[source][target] = 1;
    matrix[target][source] = 1;
  }

  // 添加社区内的负边
  for (let i = 0; i < i_n_edgeNum; i++) {
    let communityIndex, source, target;
    while (true) {
      communityIndex = rand(0, communityNum - 1);
      source = communities[communityIndex][rand(0, communities[communityIndex].length - 1)];
      target = communities[communityIndex][rand(0, communities[communityIndex].length - 1)];
      if (source !== target && matrix[source][target] === 0) {
        break;
      }
    }
    matrix[source][target] = -1;
    matrix[target][source] = -1;
  }

  // 添加社区外的正边
  for (let i = 0; i < o_p_edgeNum; i++) {
    let communityIndex1, communityIndex2, source, target;
    while (true) {
      communityIndex1 = rand(0, communityNum - 1);
      communityIndex2 = rand(0, communityNum - 1);
      if (communityIndex1 !== communityIndex2) {
        break;
      }
    }
    while (true) {
      source = communities[communityIndex1][rand(0, communities[communityIndex1].length - 1)];
      target = communities[communityIndex2][rand(0, communities[communityIndex2].length - 1)];
      if (source !== target && matrix[source][target] === 0) {
        break;
      }
    }
    matrix[source][target] = 1;
    matrix[target][source] = 1;
  }

  // 添加社区外的负边
  for (let i = 0; i < o_n_edgeNum; i++) {
    let communityIndex1, communityIndex2, source, target;
    while (true) {
      communityIndex1 = rand(0, communityNum - 1);
      communityIndex2 = rand(0, communityNum - 1);
      if (communityIndex1 !== communityIndex2) {
        break;
      }
    }
    while (true) {
      source = communities[communityIndex1][rand(0, communities[communityIndex1].length - 1)];
      target = communities[communityIndex2][rand(0, communities[communityIndex2].length - 1)];
      if (source !== target && matrix[source][target] === 0) {
        break;
      }
    }
    matrix[source][target] = -1;
    matrix[target][source] = -1;
  }
  return {
    matrix,
    communities,
  };
  // console.log(communities);
  // console.log(matrix);
}

export default generateSignedNetWork;
